package com.aizhixin.lab.course.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.domain.CourseBaseDomain;
import com.aizhixin.lab.course.course.domain.CourseChapterDomain;
import com.aizhixin.lab.course.course.entity.CourseChapter;
import com.aizhixin.lab.course.course.service.StudentChapterService;
import com.aizhixin.lab.course.course.service.StudentCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/course/student/chapter")
@Api(value = "学生课程章节相关API")
public class StudentChapterController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private StudentChapterService chapterService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节包含文件,不包含隐藏章节和文件,预览时用", response = Void.class, notes = "查询课程章节包含文件,不包含隐藏文件,预览时用<br>@author hsh")
    public ResponseEntity<?> getListWithFile(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

        List<CourseChapterDomain> result = chapterService.getListWithoutHide(courseId,account.getId());
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getChapter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "根据章节id查询章节", response = Void.class, notes = "根据章节id查询章节<br>@author hsh")
    public ResponseEntity<?> getChapter(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "chapterId", required = false) @RequestParam(value = "chapterId", required = false) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseChapter result = chapterService.getChapter(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }


}
